<template>
  <div class="login">
    <h1>登录</h1>
    <div class="login-wrapper">
      <div class="avatar">
        <img src="../assets/sun_logo.jpg" alt="">
      </div>

      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <van-field
            v-model="state.username"
            name="用户名"
            label="用户名"
            placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <van-field
            v-model="state.password"
            type="password"
            name="密码"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
        </van-cell-group>
        <div style="margin: 16px;">
          <van-button round block type="primary" native-type="submit">
            登录
          </van-button>
        </div>
      </van-form>
    </div>
    <p class="register" @click="register">新用户？点击这里注册</p>
  </div>
</template>


<script setup>
import { reactive } from 'vue'
import axios from '@/api'
import { useRouter } from 'vue-router'

const router = useRouter()
const state = reactive({
  username: '',
  password: '',
  UserDetails: [],

})

const onSubmit = async() => {
  // console.log(state.username, state.password);
  // 发接口请求，把用户输入的账号和密码传给后端
  const res = await axios.post('/users/login', {
    username: state.username,
    password: state.password
  })

  const {data} = await axios.post('/users/seekUsername',{
    username: state.username
  })
  //console.log(data);
  //拿到Proxy(Array)数组里的值，通过json序列化之后可获取值
  state.UserDetails = data // JSON.parse(JSON.stringify(data))[0]
  //console.log(state.UserDetails[0]);
  
  if (res.code === '80000') {
    sessionStorage.setItem('userInfo', JSON.stringify(res.data))
    window.sessionStorage.setItem('nickname', state.UserDetails[0].nickname)
    window.sessionStorage.setItem('headimg', state.UserDetails[0].head_img)
    router.push({
      path: '/page/mine',
    })
  }
}

const register = () => {
  router.push('/register')
}

</script>

<style lang="less" scoped>
.login{
  width: 100vw;
  height: 100vh;
  background-color: #d0e5fd;
  padding: 0 0.3rem;
  box-sizing: border-box;
  overflow: hidden;
  position: relative;
 
  h1{
    height: 0.6933rem;
    text-align: center;
    margin-top: 1.12rem;
    font-size: 0.48rem;
  }
  .login-wrapper{
    width: 7.44rem;
    height: 10.77rem;
    border: 1px solid rgba(187, 187, 187, 1);
    margin: 0 auto;
    margin-top: 1.7rem;
    border-radius: 0.3rem;
    box-shadow: 0 0 0.533rem 0 rgba(170, 170, 170, 1);
    .avatar{
      width: 2.4rem;
      height: 2.4rem;
      overflow: hidden;
      border-radius: 50%;
      margin: 1rem auto 0.77rem;
      img{
        width: 100%;
      }
    }
  }
  .register{
    position: absolute;
    bottom: 30px;
    width: 80%;
    left: 50%;
    text-align: center;
    font-size: 0.374rem;
    transform: translateX(-50%);
    height: 0.6rem;
    line-height: 0.6rem;
    color: rgba(16, 16, 16, 1);
  }
}
</style>
<style>
.van-cell__title.van-field__label{
  width: 45px;
}
</style>